<?php
/**
 * 订单管理
 * @author maniac
 */
namespace app\manage\controller;

//use app\facade\Orders AS OrdersFacade;
use app\common\model\PointOrders AS PointOrdersModel;
//use app\facade\UserLogs;

class PointOrders extends Base
{ 
    /**
     * 详情页附加
     * @param array $info 查看的信息
     */
    public function addAttach($info)
    {
        
    }
    
    /**
     * 组合 where 查询条件
     */
    public function where()
    {
        $order_status_id = input('get.order_status_id/d', 0);
        $keywords        = input('get.keywords/s', '');
        $startTime       = input('get.start_time/s', '');
        $endTime         = input('get.end_time/s', '');
        $where           = [];
        if ($order_status_id) {
            switch ($order_status_id) {
                case 1:
                    // 已取消
                    $where[] = [
                        ['order_status', 'eq', 2],
                    ];
                    break;
                case 2:
                    // 未付款
                    $where[] = [
                        ['order_status', 'eq', 1],
                        ['pay_status', 'eq', 0],
                    ];
                    break;
                case 3:
                    // 已付款
                    $where[] = [
                        ['order_status', 'eq', 1],
                        ['pay_status', 'eq', 1],
                    ];
                    break;
                case 4:
                    // 待发货
                    $where[] = [
                        ['order_status', 'eq', 1],
                        ['pay_status', 'eq', 1],
                        ['shipping_status', 'eq', 0],
                    ];
                    break;
                case 5:
                    // 待收货
                    $where[] = [
                        ['order_status', 'eq', 1],
                        ['pay_status', 'eq', 1],
                        ['shipping_status', 'eq', 1],
                    ];
                    break;
                case 6:
                    $where[] = [
                        ['order_status', 'eq', 255],
                    ];
                    // 已完成
                    break;
            }
        }
        
        if ($keywords) {
            $where[] = [
                ['username', 'like', '%'.$keywords.'%'],
            ];
        }
        
        if ($startTime) {
            $where[] = [
                ['create_time', 'egt', strtotime($startTime)],
            ];
        }
        if ($endTime) {
            $where[] = [
                ['create_time', 'lt', strtotime($endTime)],
            ];
        }
        
        $this->assign('order_status_id', $order_status_id);
        $this->assign('keywords', $keywords);
        $this->assign('startTime', $startTime);
        $this->assign('endTime', $endTime);
        return $where;
    }
    
//     /**
//      * 导出 excel
//      * @return [type] [description]
//      */
//     public function export()
//     {
//         $where = $this->where();
//         UserLogs::insertLog(['title' => '导出订单列表']);
//         OrdersFacade::export($where);
//     }
    
//     /**
//      * 指派配送员
//      */
//     public function allot()
//     {
//         $id     = input('post.id/d', 0);
//         $userid = input('post.userid/d', 0);
//         if (!$id || !$userid) {
//             return j(1, '指派失败');
//         }
//         try {
//             $result = OrdersFacade::allot($id, $userid);
//             if ($result) {
//                 return j(0, '指派成功');
//             }
//             return j(1, '指派失败');
//         } catch (\Exception $e) {
//             return j(1, $e->getMessage());
//         }
//     }
    
    /**
     * 订单操作
     */
    public function orderAction()
    {
        $data = input('post.');
        if (isset($data['shipping_status'])) {
        	if ($data['shipping_status'] == 2) {
        		$data['order_status'] = 255;
        		$data['sign_time'] = time();
        		$data['finish_time'] = time();
        	}else{
        		$data['send_time'] = time();
        		$data['courier'] = $data['value'];
        	} 
        }
        if (isset($data['pay_status'])) {
        	$data['pay_time'] = time();
        
        }
        PointOrdersModel::update($data);
        return j(0, '操作成功');
    }
}
